package com.servoy.r3.plugins;

import org.w3c.dom.*;

public class JRXMLSubreport {

	public final static String JRXML_SUBREPORT_EXPRESSION = "subreportExpression";
	public final static String JRXML_XML_EXTENSION = ".jrxml";
	public final static String JRXML_JASPER_EXTENSION_WITHOUTDOT = "jasper";
	public final static String JRXML_JASPER_EXTENSION = "."
			+ JRXML_JASPER_EXTENSION_WITHOUTDOT;

	private Element xmlElement;

	public JRXMLSubreport(Element xmlelement) {

		xmlElement = xmlelement;
	}

	public void updateSubreportExpresionForXML() {
		// Get JR sql query
		NodeList nodes = xmlElement
				.getElementsByTagName(JRXML_SUBREPORT_EXPRESSION);

		if (nodes.getLength() > 0) {
			Element subrexprelement = (Element) nodes.item(0);

			NodeList qchlds = subrexprelement.getChildNodes();
			for (int k = 0; k < qchlds.getLength(); k++) {
				Node n = qchlds.item(k);
				if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
					String ss = n.getNodeValue();

					// Remove " from the beginning and end of the string value.
					//String ssubrepexpr = ss.substring(1, ss.length() - 1);
					String ssubrepexpr = Utils.getNameWithoutQuotes(ss);

					String name = "\"\"";
					
					if (!ssubrepexpr.equals("")) 
						name = "\""
								+ Utils
										.getNameWithoutExtensionFromFileName(ssubrepexpr)
								+ JRXMLProvider.XMLREPORT_XMLED + "."
								+ Utils.getExtensionFromFileName(ssubrepexpr)
								+ "\"";

					n.setNodeValue(name);
				}
			}
		}
	}

	public void updateSubreportExpressionForSQL(String filename) {
		// Get JR sql query
		NodeList nodes = xmlElement
				.getElementsByTagName(JRXML_SUBREPORT_EXPRESSION);

		if (nodes.getLength() > 0) {
			Element subrexprelement = (Element) nodes.item(0);

			NodeList qchlds = subrexprelement.getChildNodes();
			for (int k = 0; k < qchlds.getLength(); k++) {
				Node n = qchlds.item(k);
				if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
					n.setNodeValue("\"" + filename + "\"");
				}
			}
		}
	}

	public String getSubreportFilename(boolean changeExtension) {

		String subrepexpr = "";

		// Get JR sql query
		NodeList nodes = xmlElement
				.getElementsByTagName(JRXML_SUBREPORT_EXPRESSION);

		if (nodes.getLength() > 0) {
			Element subrexprelement = (Element) nodes.item(0);

			NodeList qchlds = subrexprelement.getChildNodes();
			for (int k = 0; k < qchlds.getLength(); k++) {
				Node n = qchlds.item(k);
				if (n.getNodeType() == Node.CDATA_SECTION_NODE) {
					String ss = n.getNodeValue();
					
					// Remove " from the beginning and end of the string value.
					//String ssubrepexpr = ss.substring(1, ss.length() - 1);
					String ssubrepexpr = Utils.getNameWithoutQuotes(ss);

					if (changeExtension
							&& (Utils.getExtensionFromFileName(ssubrepexpr)
									.compareToIgnoreCase(
											JRXML_JASPER_EXTENSION_WITHOUTDOT) == 0)) {
						int lastindex = ssubrepexpr
								.lastIndexOf(JRXML_JASPER_EXTENSION);
						String s = ssubrepexpr.substring(0, lastindex);
						StringBuffer f = new StringBuffer(s);
						f.append(JRXML_XML_EXTENSION);
						subrepexpr = f.toString();
					} else {
						subrepexpr = ssubrepexpr;
					}

					break;
				}
			}
		}

		return subrepexpr;
	}

}
